package com.feeyin.radar.crm.dao.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.LinkmanDAO;
import com.feeyin.radar.crm.model.Linkman;
import com.feeyin.radar.sys.dao.CountryDAO;
import com.feeyin.radar.sys.dao.DataDictionaryDAO;
import com.feeyin.radar.sys.dao.RecordRangeDAO;
import com.feeyin.radar.sys.model.DataDictionary;
import com.feeyin.radar.sys.model.RecordRange;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class LinkmanDAOImpl extends SqlMapClientDaoSupport
  implements LinkmanDAO
{
  private DataDictionaryDAO dataDictionaryDAO;
  private RecordRangeDAO recordRangeDAO;
  private CountryDAO countryDAO;

  public DataDictionaryDAO getDataDictionaryDAO()
  {
    return this.dataDictionaryDAO;
  }

  public void setDataDictionaryDAO(DataDictionaryDAO dataDictionaryDAO) {
    this.dataDictionaryDAO = dataDictionaryDAO;
  }

  public RecordRangeDAO getRecordRangeDAO() {
    return this.recordRangeDAO;
  }

  public void setRecordRangeDAO(RecordRangeDAO recordRangeDAO) {
    this.recordRangeDAO = recordRangeDAO;
  }

  public CountryDAO getCountryDAO() {
    return this.countryDAO;
  }

  public void setCountryDAO(CountryDAO countryDAO) {
    this.countryDAO = countryDAO;
  }

  public Integer getDeptCustomerLinkManCount(Linkman linkman)
  {
    return (Integer)getSqlMapClientTemplate().queryForObject("getDeptCustomerLinkManCount", linkman);
  }

  public List getDeptCustomerLinkMan(Linkman linkman) {
    return getSqlMapClientTemplate().queryForList("getDeptCustomerLinkMan", linkman);
  }
  public List getLinkmansByCustomerId(Integer customerId) {
    return getSqlMapClientTemplate().queryForList("getLinkmansByCustomerId", customerId);
  }

  public List getLinkmansByDynamic(Linkman linkman) {
    if (linkman == null) return null;
    return getSqlMapClientTemplate().queryForList("getLinkmansByDynamic", linkman);
  }

  public Map getLinkmanDictionaryRanges() {
    Map map = new HashMap();

    List linkmanDictionarys = new ArrayList();
    linkmanDictionarys = this.dataDictionaryDAO.getDataDictionarysByRecordType("2");
    if (linkmanDictionarys != null) {
      for (int i = 0; i < linkmanDictionarys.size(); i++) {
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary = (DataDictionary)linkmanDictionarys.get(i);
        List ranges = this.recordRangeDAO.getActivityRecordRangesByDictionaryId(dataDictionary.getId());
        if ("217".equals(dataDictionary.getRecordCode()))
          ranges = this.countryDAO.getCountrysByParentId(Integer.valueOf(0));
        else if ("218".equals(dataDictionary.getRecordCode()))
          ranges = this.countryDAO.getCountrysByLevel(Integer.valueOf(2));
        else if ("219".equals(dataDictionary.getRecordCode())) {
          ranges = this.countryDAO.getCountrysByLevel(Integer.valueOf(3));
        }
        map.put(dataDictionary.getRecordCode(), ranges);
      }
    }
    return map;
  }

  public Linkman getLinkman(Integer id) {
    return (Linkman)getSqlMapClientTemplate().queryForObject("getLinkman", id);
  }

  public Linkman getLinkmanByLinkmanCode(String linkmanCode) {
    return (Linkman)getSqlMapClientTemplate().queryForObject("getLinkmanByLinkmanCode", linkmanCode);
  }

  public Integer linkmanDeptId(Integer deptDictionaryId, String linkmanDept) {
    if (deptDictionaryId == null) return Integer.valueOf(0);
    if (StringUtils.isEmpty(linkmanDept)) return Integer.valueOf(0);
    Integer linkmanDeptId = Integer.valueOf(0);
    List list = new ArrayList();
    list = this.recordRangeDAO.getActivityRecordRangesByDictionaryIdAndRangeValue(deptDictionaryId, linkmanDept);
    if ((list != null) && (list.size() > 0)) {
      linkmanDeptId = ((RecordRange)list.get(0)).getId();
    }
    else {
      RecordRange recordRange = new RecordRange();
      recordRange.setRangeCode(DateUtils.getDateByFormat("yyyyMMddHHmmss") + "dept" + new Date().getTime());
      recordRange.setDataDictionaryId(deptDictionaryId);
      recordRange.setRangeValue(linkmanDept);
      this.recordRangeDAO.insertRecordRange(recordRange);

      list = new ArrayList();
      list = this.recordRangeDAO.getActivityRecordRangesByDictionaryIdAndRangeValue(deptDictionaryId, linkmanDept);
      if ((list != null) && (list.size() > 0)) {
        linkmanDeptId = ((RecordRange)list.get(0)).getId();
      }
    }

    return linkmanDeptId;
  }

  public Integer linkmanDutyId(Integer dutyDationaryId, String linkmanDuty) {
    if (dutyDationaryId == null) return Integer.valueOf(0);
    if (StringUtils.isEmpty(linkmanDuty)) return Integer.valueOf(0);
    Integer linkmanDutyId = Integer.valueOf(0);
    List list = new ArrayList();
    list = this.recordRangeDAO.getActivityRecordRangesByDictionaryIdAndRangeValue(dutyDationaryId, linkmanDuty);
    if ((list != null) && (list.size() > 0)) {
      linkmanDutyId = ((RecordRange)list.get(0)).getId();
    }
    else {
      RecordRange recordRange = new RecordRange();
      recordRange.setRangeCode(DateUtils.getDateByFormat("yyyyMMddHHmmss") + "duty" + new Date().getTime());
      recordRange.setDataDictionaryId(dutyDationaryId);
      recordRange.setRangeValue(linkmanDuty);
      this.recordRangeDAO.insertRecordRange(recordRange);

      list = new ArrayList();
      list = this.recordRangeDAO.getActivityRecordRangesByDictionaryIdAndRangeValue(dutyDationaryId, linkmanDuty);
      if ((list != null) && (list.size() > 0)) {
        linkmanDutyId = ((RecordRange)list.get(0)).getId();
      }
    }
    return linkmanDutyId;
  }

  public boolean insertLinkman(Linkman linkman) {
    if (linkman == null) return false;
    if (StringUtils.isEmpty(linkman.getLinkmanCode())) return false;
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertLinkman", linkman);
    return getLinkmanByLinkmanCode(linkman.getLinkmanCode()) != null;
  }

  public boolean updateLinkman(Linkman linkman)
  {
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateLinkman", linkman));
    return result != null;
  }

  public boolean updateLinkmanByStaticParam(Linkman linkman)
  {
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateLinkmanByStaticParam", linkman));
    return result != null;
  }

  public List getLinkManMailInfo(Linkman linkman) {
    return getSqlMapClientTemplate().queryForList("getLinkManMailInfo", linkman);
  }

  public Integer getCountLinkManMailInfo(Linkman linkman) {
    return (Integer)getSqlMapClientTemplate().queryForObject("getCountLinkManMailInfo", linkman);
  }
}